package com.sina.shop.business;

import com.sina.shop.domain.business.Business;
import com.sina.shop.service.business.BusinessService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-07-17:13
 */
@RestController
@RequestMapping("/business")
public class BusinessController extends BaseController {

    @Autowired
    @Qualifier("businessService")
    private BusinessService businessService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getBusinessListByAdminId", method = RequestMethod.GET)
    public List<Business> getBusinessListByAdminId(@RequestParam(value = "adminId") Long adminId) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getBusinessListByAdminId, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return businessService.getBusinessByAdminId(adminId);
    }

    @RequestMapping(value = "/getBusinessList", method = RequestMethod.GET)
    public Map<String, Object> getBusinessList(@RequestBody(required = false) Business business) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getBusinessList, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());

        Map<String, Object> map = new HashMap<>(4);
        Long count[] = new Long[1];
        List<Business> businessList = businessService.getBusinessList(business, count);
        map.put("count", count[0]);
        map.put("businessList", businessList);

        return map;
    }

    @RequestMapping(value = "/deleteBusiness", method = RequestMethod.POST)
    public Integer deleteBusiness(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/deleteBusiness, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return businessService.deleteBusinessTab(id);
    }

    @RequestMapping(value = "/saveBusiness", method = RequestMethod.POST)
    public Business saveBusiness(@RequestBody(required = false) Business business) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/saveBusiness, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return businessService.saveBusiness(business);
    }

    @RequestMapping(value = "/updateBusiness", method = RequestMethod.POST)
    public Business updateBusiness(@RequestBody(required = false) Business business) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/updateBusiness, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return businessService.updateBusiness(business);
    }

}
